import 'package:flame/events.dart' as events;
import 'package:flame/extensions.dart';
import 'package:flame/game.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_shader/generated/assets.dart';
import 'package:flutter_shader/widgets/game.dart';

/// Created by changlei on 2024/2/22.
///
/// 大海
class WaterPage extends StatelessWidget {
  /// 大海
  const WaterPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GameScaffold(
      assetKey: Assets.shaders(Shaders.water),
      builder: _Game.new,
    );
  }
}

final class _Game extends ShaderGame {
  _Game(super.shader);

  double time = 0;
  Vector2 mouse = Vector2.zero();

  @override
  void onDragUpdate(events.DragUpdateEvent event) {
    super.onDragUpdate(event);
    mouse += event.localDelta;
    shader.setFloat(3, mouse.x);
    shader.setFloat(4, mouse.y);
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    shader.setFloat(0, size.x);
    shader.setFloat(1, size.y);
  }

  @override
  void update(double dt) {
    super.update(dt);
    time += dt;
    shader.setFloat(2, time);
  }
}
